home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_13_05
/
allison
/
person2.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-12
|
1KB
|
58 lines
LISTING 20 - The implementation for Listing 19, which is missing
an assignment operator and copy constructor
// person2.cpp
#include <iostream.h>
#include <string.h>
#include <new.h>
#include "person2.h"
static char * clone(const char *s)
{
// return a copy of a string on the heap
char * p = new char[strlen(s) + 1];
return strcpy(p,s);
}
Person::Person() : birth(Date(0,0,0))
{
last = clone("");
first = clone("");
ssn = clone("");
}
Person::Person(const char * l, const char * f,
const Date& b, const char * s)
: birth(b)
{
last = clone(l);
first = clone(f);
ssn = clone(s);
}
Person::~Person()
{
delete [] last;
delete [] first;
delete [] ssn;
}
ostream& operator<<(ostream& os, const Person& p)
{
os << '{'
<< p.last << ','
<< p.first << ','
<< '[' << p.birth << ']' << ','
<< p.ssn
<< '}';
return os;
}
bool Person::operator==(const Person & p) const
{
return strcmp(last,p.last) == 0 &&
strcmp(first,p.first) == 0 &&
birth == p.birth &&
strcmp(ssn,p.ssn) == 0;
}